﻿using PoemService.Shared;

namespace PoemGame.Domain.Services.Jielong
{
    public class JielongCheckAnswerService : ICheckAnswerService
    {
        protected readonly IPoemService poemService;
        public JielongCheckAnswerService(IPoemService _poemService)
        {
            poemService = _poemService;
        }
        public virtual async Task<bool> CheckAnswer(CheckAnswerServiceInput game, string answer)
        {
            var records = game.ProperAnswers;
            var preanswer = "";
            if (records.Count == 0) preanswer = game.GameCondition;
            else preanswer = records[records.Count - 1];
            if (records.Find(o => o == answer) != null) return false;
            if (!await poemService.IsPoemLineExist(answer)) return false;
            return (preanswer.Substring(preanswer.Length - 1) == answer.Substring(0, 1));
        }
    }
}